Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports C4F.DevKit.PictureAcquisition
Namespace PictureAcquisitionApplication
	Public Partial Class DeviceItemsDetails
	Inherits Form
		#Region "Globals"
		Private pictureAquisitionManager As PictureAcquisitionManager = Nothing
		Private deviceIdNew As String = String.Empty
#End Region

		#Region "Constructor"
		Public Sub New()
			InitializeComponent()
		End Sub
		Public Sub New(ByVal manager As PictureAcquisitionManager, ByVal deviceId As String)
			deviceIdNew = deviceId
			pictureAquisitionManager = manager
			InitializeComponent()
			GetDeviceItemsDetails(deviceIdNew)
		End Sub
#End Region

		#Region "Helper Methods"
		Private Sub GetDeviceItemsDetails(ByVal deviceIdNew As String)
			Try
				Dim deviceItems As List(Of String) = New List(Of String)()
				deviceItems = pictureAquisitionManager.GetDeviceItems(deviceIdNew)
				For Each dev As String In deviceItems
					dgViewItemName.Rows.Add(dev)
				Next
			Catch ex As ArgumentNullException
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentException
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As PictureAcquisitionException
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		Private Sub DeleteDeviceItem(ByVal deviceIdNew As String)
			Try
				Dim rows As DataGridViewSelectedRowCollection = dgViewItemName.SelectedRows
				For Each r As DataGridViewRow In rows
					Dim itemName As String = r.Cells(0).Value.ToString()
					pictureAquisitionManager.DeleteItem(itemName, deviceIdNew)
					dgViewItemName.Rows.Remove(r)
				Next
			Catch ex As ArgumentNullException
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentException
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As PictureAcquisitionException
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		Private Sub AcquireSelectedDeviceItems(ByVal deviceIdNew As String)
			Try
				Dim rows As DataGridViewSelectedRowCollection = dgViewItemName.SelectedRows
				Dim selectedItems As List(Of String) = New List(Of String)()
				For Each r As DataGridViewRow In rows
					Dim itemName As String = r.Cells(0).Value.ToString()
					selectedItems.Add(itemName)
				Next
				Dim fileBrowserDialog As FolderBrowserDialog = New FolderBrowserDialog()
				fileBrowserDialog.Description = "Choose folder to import selected files"
				fileBrowserDialog.ShowNewFolderButton = True
                If Windows.Forms.DialogResult.OK = fileBrowserDialog.ShowDialog() Then
                    pictureAquisitionManager.AcquireItems(deviceIdNew, selectedItems, fileBrowserDialog.SelectedPath + "\")
                End If
			Catch ex As ArgumentNullException
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentException
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As PictureAcquisitionException
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
#End Region

		#Region "Event Handler"
        Private Sub btnRefreshDeviceItems_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRefreshDeviceItems.Click
            Try
                dgViewItemName.Rows.Clear()
                GetDeviceItemsDetails(deviceIdNew)
            Catch ex As ArgumentNullException
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Catch ex As ArgumentException
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Catch ex As PictureAcquisitionException
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            End Try
        End Sub
        Private Sub btnDeleteDeviceItems_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDeleteDeviceItems.Click
            Try
                DeleteDeviceItem(deviceIdNew)
            Catch ex As ArgumentNullException
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Catch ex As ArgumentException
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Catch ex As PictureAcquisitionException
                MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            End Try
        End Sub

        Private Sub btnCloseDeviceItems_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCloseDeviceItems.Click
            Me.Close()
        End Sub
        Private Sub DeviceItemsDetails_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
            If e.KeyCode = Keys.Escape Then
                Me.Close()
            End If
        End Sub
        Private Sub btnDeviceItemDetailsImport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDeviceItemDetailsImport.Click
            AcquireSelectedDeviceItems(deviceIdNew)
        End Sub
#End Region

    End Class
End Namespace
